<template>
  <div class="app-container"> 
    <div>
     <el-button type="primary" style="margin-top: 10px; margin-left: 10px"
           @click="handleAdd()" >新增</el-button>
    <div style="margin-top: 10px; margin-left: 10px; width: 280px; float: right;">
    <el-input placeholder="地址信息" prefix-icon="el-icon-search" 
    v-model="addressQuery" style="width: 230px;"> </el-input>
    <el-button icon="el-icon-search" circle clearable style="float: right;" @click="fetchData()"></el-button>
  </div>
  </div>
    <el-table :data="list" v-loading="listLoading" style="width: 100%" :row-class-name="tableRowClassName" 
    highlight-current-row
    @current-change="checkChange">
      <el-table-column
      type="index"
      width="100">
    </el-table-column>
      <el-table-column prop="id" label="地址编号" >
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column prop="province" label="省" >
        <template slot-scope="scope">
          {{ scope.row.province }}
        </template>
      </el-table-column>
      <el-table-column prop="city" label="市" >
        <template slot-scope="scope">
          {{ scope.row.city }}
        </template>
      </el-table-column>
      <el-table-column prop="area" label="区" >
        <template slot-scope="scope">
          {{ scope.row.area }}
        </template>
      </el-table-column>
      <el-table-column prop="addressdetail" label="详细地址" >
        <template slot-scope="scope">
          {{ scope.row.addressdetail }}
        </template>
      </el-table-column>
      <el-table-column prop="contactname" label="联系人" >
        <template slot-scope="scope">
          {{ scope.row.contactname }}
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号码" >
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column prop="isdefault" label="是否默认" >
        <template slot-scope="scope">
          {{ scope.row.isdefault }}
        </template>
      </el-table-column>
      <el-table-column prop="userid" label="用户" >
        <template slot-scope="scope">
          {{ scope.row.userid }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作"  align="center" width="330px">
        <template slot-scope="scope">
           <el-button type="primary" style="margin-top: 10px; margin-left: 10px"
           @click="handleEdit(scope.$index, scope.row)" >编辑</el-button>
          <el-button type="danger" style="margin-top: 10px; margin-left: 10px"
          @click="dialogVisible = true" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[10, 20, 30, 400]"
      :page-size="1"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <span>确认删除?一经删除无法恢复！</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleClose()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
  </div>
</template>


<script>
import { addressList, deleteAddress } from "@/api/address/address";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      currentRow: null,
      redirect: null,
      total: 0,
      currentPage4: 1,
      dialogVisible: false,
      addressQuery: '',
      from: {
        page : 1,
        row: 10,
        addressdetail: ''
      }
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    // 设置行的颜色
    tableRowClassName({row, rowIndex}) {
        // if (rowIndex === 1) {
        //   return 'warning-row';
        // } else if (rowIndex === 3) {
        //   return 'success-row';
        // }
        if(rowIndex%2 == 0){
            return 'warning-row';
        }else{
            return 'success-row';
        }
        return '';
    },
    // 选中哪一行
    checkChange(val) {
        console.log('选中的数据'+ JSON.stringify(val))
        console.log('dialogVisible===', this.dialogVisible)
        this.currentRow = val;
        this.fetchData();
    },
    handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.from.row = val
        this.fetchData();
    },
    handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.from.page = val
        this.fetchData();
    },
    // 列表数据
    fetchData() {
      this.listLoading = true
      console.log('this.from===', JSON.stringify(this.from))
      this.from.addressdetail = this.addressQuery
      addressList(this.from).then(res => {
        this.list = res.data.data;
        this.row = res.data.row
        this.page = res.data.page
        this.total = res.data.total
        this.listLoading = false;
      });
    },
    // 地址新增
    handleAdd(){
      this.$router.push({path : "/address/operate"});
    },
    // 地址编辑
    handleEdit(index, row){
      console.log('地址编辑 index=[' + index + '], row=[' + JSON.stringify(row) +']')
      this.$router.push({path : "/address/operate", query : row});
    },
    // 地址删除
    handleClose(done) {
      deleteAddress(this.currentRow).then(res => {
          console.log("地址删除返回信息==" + JSON.stringify(res))
          if (res.code === 200) {
            console.log("code === 200，地址删除成功");
            this.dialogVisible = false 
            // done();
            location.reload()
          }
      })
        // this.$confirm('确认执行该操作，一经删除无法恢复！')
        // .then(_ => {
        //   })
        //   .catch(_ => {
        //     console.log('地址删除 ERROR')
        //   });
      }
  }
}
</script>

<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
  .block {
    float: right;
    height: 40px;
  }
</style>
